import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
 
 
class StackedExample(QWidget): #直接一整个屏幕就是一个选项卡窗口
    def __init__(self,parent=None):
        super(StackedExample, self).__init__(parent)
        self.setWindowTitle('堆栈窗口控件:QStackedWidget')
        #self.resize(400,200)
        self.setGeometry(800,450,10,10)
        #列表控件
        self.list=QListWidget()
        self.list.insertItem(0,'联系方式')
        self.list.insertItem(1,'个人信息')
        self.list.insertItem(2,'教育程度')
 
        self.stack1=QWidget()
        self.stack2 = QWidget()
        self.stack3 = QWidget()
 
        self.tab1UI()
        self.tab2UI()
        self.tab3UI()
        #堆栈窗口控件对象
        self.stack=QStackedWidget()
        self.stack.addWidget(self.stack1)
        self.stack.addWidget(self.stack2)
        self.stack.addWidget(self.stack3)
 
        self.list.currentRowChanged.connect(self.display)
 
        hbox=QHBoxLayout()
        hbox.addWidget(self.list)
        hbox.addWidget(self.stack)
        self.setLayout(hbox)
 
    def tab1UI(self):
        #表单布局
        layout=QFormLayout()
        layout.addRow('姓名',QLineEdit())
        layout.addRow('地址',QLineEdit())
        self.stack1.setLayout(layout)#别忘了tab1就是一个窗口
 
    def tab2UI(self):
        layout=QFormLayout()
        sex=QHBoxLayout()#水平布局，横向排列
        #单选框
        sex.addWidget(QRadioButton('男'))
        sex.addWidget(QRadioButton('女'))
        layout.addRow(QLabel('性别'),sex)
        layout.addRow('生日',QLineEdit())
        self.stack2.setLayout(layout)
 
    def tab3UI(self):
        layout=QHBoxLayout()
        layout.addWidget(QLabel('科目'))
        #复选框
        layout.addWidget(QCheckBox('物理'))
        layout.addWidget(QCheckBox('高数'))
        self.stack3.setLayout(layout)
 
    def display(self,index):
        #通过索引来切换页面
        self.stack.setCurrentIndex(index)
 
 
if __name__=='__main__':
    app=QApplication(sys.argv)
    main=StackedExample()
    main.show()
    sys.exit(app.exec_())
 
